package com.decompiler.ui;

import javax.swing.*;
import java.awt.*;

/**
 * 自定义圆角面板
 */
public class RoundedPanel extends JPanel {
    private Color backgroundColor;
    private int cornerRadius;
    private boolean dropShadow;
    private int shadowSize = 5;
    
    /**
     * 创建一个圆角面板
     * 
     * @param layout 布局管理器
     * @param backgroundColor 背景颜色
     * @param cornerRadius 圆角半径
     * @param dropShadow 是否添加阴影
     */
    public RoundedPanel(LayoutManager layout, Color backgroundColor, int cornerRadius, boolean dropShadow) {
        super(layout);
        this.backgroundColor = backgroundColor;
        this.cornerRadius = cornerRadius;
        this.dropShadow = dropShadow;
        setOpaque(false);
    }
    
    /**
     * 创建一个简单的圆角面板
     * 
     * @param layout 布局管理器
     * @param backgroundColor 背景颜色
     * @param cornerRadius 圆角半径
     */
    public RoundedPanel(LayoutManager layout, Color backgroundColor, int cornerRadius) {
        this(layout, backgroundColor, cornerRadius, false);
    }
    
    /**
     * 创建一个简单的圆角面板，使用默认布局
     * 
     * @param backgroundColor 背景颜色
     * @param cornerRadius 圆角半径
     */
    public RoundedPanel(Color backgroundColor, int cornerRadius) {
        this(new BorderLayout(), backgroundColor, cornerRadius, false);
    }
    
    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        
        Graphics2D g2d = (Graphics2D) g.create();
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        
        // 计算阴影区域
        int shadowOffset = dropShadow ? shadowSize : 0;
        int width = getWidth() - shadowOffset * 2;
        int height = getHeight() - shadowOffset * 2;
        
        // 绘制阴影
        if (dropShadow) {
            g2d.setColor(new Color(0, 0, 0, 50));
            g2d.fillRoundRect(shadowOffset, shadowOffset, width, height, cornerRadius, cornerRadius);
        }
        
        // 绘制面板背景
        g2d.setColor(backgroundColor);
        g2d.fillRoundRect(0, 0, width, height, cornerRadius, cornerRadius);
        
        g2d.dispose();
    }
    
    @Override
    public Dimension getPreferredSize() {
        Dimension size = super.getPreferredSize();
        if (dropShadow) {
            size.width += shadowSize * 2;
            size.height += shadowSize * 2;
        }
        return size;
    }
    
    /**
     * 设置背景颜色
     * 
     * @param color 背景颜色
     */
    public void setBackgroundColor(Color color) {
        this.backgroundColor = color;
        repaint();
    }
    
    /**
     * 设置圆角半径
     * 
     * @param radius 圆角半径
     */
    public void setCornerRadius(int radius) {
        this.cornerRadius = radius;
        repaint();
    }
    
    /**
     * 设置是否显示阴影
     * 
     * @param dropShadow 是否显示阴影
     */
    public void setDropShadow(boolean dropShadow) {
        this.dropShadow = dropShadow;
        repaint();
    }
    
    /**
     * 设置阴影大小
     * 
     * @param shadowSize 阴影大小
     */
    public void setShadowSize(int shadowSize) {
        this.shadowSize = shadowSize;
        repaint();
    }
}